<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Config_Xml - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.config.adapters.xml.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.config.adapters.xml.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.config.adapters.json.html">Zend_Config_Json</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.config.html">Zend_Config</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.config.adapters.yaml.html">Zend_Config_Yaml</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.config.adapters.xml" class="section"><div class="info"><h1 class="title">Zend_Config_Xml</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Config_Xml</span> enables developers to store configuration data in a
        simple <acronym class="acronym">XML</acronym> format and read them via nested object property syntax. The
        root element of the <acronym class="acronym">XML</acronym> file or string is irrelevant and may be named
        arbitrarily. The first level of <acronym class="acronym">XML</acronym> elements correspond with
        configuration data sections. The <acronym class="acronym">XML</acronym> format supports hierarchical
        organization through nesting of <acronym class="acronym">XML</acronym> elements below the section-level
        elements. The content of a leaf-level <acronym class="acronym">XML</acronym> element corresponds to the
        value of a configuration datum. Section inheritance is supported by a special
        <acronym class="acronym">XML</acronym> attribute named <em class="emphasis">extends</em>, and the value of this
        attribute corresponds with the section from which data are to be inherited by the extending
        section.
    </p>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Return Type</b><br /></span>
        

        <p class="para">
            Configuration data read into <span class="classname">Zend_Config_Xml</span> are always returned
            as strings. Conversion of data from strings to other types is left to developers to
            suit their particular needs.
        </p>
    </p></blockquote>

    <div class="example" id="zend.config.adapters.xml.example.using"><div class="info"><p><b>Example #1 Using Zend_Config_Xml</b></p></div>
        

        <div class="example-contents"><p>
            This example illustrates a basic use of <span class="classname">Zend_Config_Xml</span> for
            loading configuration data from an <acronym class="acronym">XML</acronym> file. In this example there
            are configuration data for both a production system and for a staging system. Because
            the staging system configuration data are very similar to those for production, the
            staging section inherits from the production section. In this case, the decision is
            arbitrary and could have been written conversely, with the production section
            inheriting from the staging section, though this may not be the case for more complex
            situations. Suppose, then, that the following configuration data are contained in
            <var class="filename">/path/to/config.xml</var>:
        </p></div>

        <pre class="programlisting brush: xml">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;configdata&gt;
    &lt;production&gt;
        &lt;webhost&gt;www.example.com&lt;/webhost&gt;
        &lt;database&gt;
            &lt;adapter&gt;pdo_mysql&lt;/adapter&gt;
            &lt;params&gt;
                &lt;host&gt;db.example.com&lt;/host&gt;
                &lt;username&gt;dbuser&lt;/username&gt;
                &lt;password&gt;secret&lt;/password&gt;
                &lt;dbname&gt;dbname&lt;/dbname&gt;
            &lt;/params&gt;
        &lt;/database&gt;
    &lt;/production&gt;
    &lt;staging extends=&quot;production&quot;&gt;
        &lt;database&gt;
            &lt;params&gt;
                &lt;host&gt;dev.example.com&lt;/host&gt;
                &lt;username&gt;devuser&lt;/username&gt;
                &lt;password&gt;devsecret&lt;/password&gt;
            &lt;/params&gt;
        &lt;/database&gt;
    &lt;/staging&gt;
&lt;/configdata&gt;
</pre>


        <div class="example-contents"><p>
            Next, assume that the application developer needs the staging configuration data from
            the <acronym class="acronym">XML</acronym> file. It is a simple matter to load these data by specifying
            the <acronym class="acronym">XML</acronym> file and the staging section:
        </p></div>

        <pre class="programlisting brush: php">
$config = new Zend_Config_Xml(&#039;/path/to/config.xml&#039;, &#039;staging&#039;);

echo $config-&gt;database-&gt;params-&gt;host;   // prints &quot;dev.example.com&quot;
echo $config-&gt;database-&gt;params-&gt;dbname; // prints &quot;dbname&quot;
</pre>

    </div>

    <div class="example" id="zend.config.adapters.xml.example.attributes"><div class="info"><p><b>Example #2 Using Tag Attributes in Zend_Config_Xml</b></p></div>
        

        <div class="example-contents"><p>
            <span class="classname">Zend_Config_Xml</span> also supports two additional ways of defining
            nodes in the configuration. Both make use of attributes. Since the
            <em class="emphasis">extends</em> and the <em class="emphasis">value</em> attributes are reserved
            keywords (the latter one by the second way of using attributes), they may not be
            used. The first way of making usage of attributes is to add attributes in a parent
            node, which then will be translated into children of that node:
        </p></div>

        <pre class="programlisting brush: xml">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;configdata&gt;
    &lt;production webhost=&quot;www.example.com&quot;&gt;
        &lt;database adapter=&quot;pdo_mysql&quot;&gt;
            &lt;params host=&quot;db.example.com&quot; username=&quot;dbuser&quot; password=&quot;secret&quot;
                    dbname=&quot;dbname&quot;/&gt;
        &lt;/database&gt;
    &lt;/production&gt;
    &lt;staging extends=&quot;production&quot;&gt;
        &lt;database&gt;
            &lt;params host=&quot;dev.example.com&quot; username=&quot;devuser&quot;
                    password=&quot;devsecret&quot;/&gt;
        &lt;/database&gt;
    &lt;/staging&gt;
&lt;/configdata&gt;
</pre>


        <div class="example-contents"><p>
            The other way does not really shorten the config, but keeps it easier to maintain since
            you don&#039;t have to write the tag name twice. You simply create an empty tag with the
            value in the <em class="emphasis">value</em> attribute:
        </p></div>

        <pre class="programlisting brush: xml">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;configdata&gt;
    &lt;production&gt;
        &lt;webhost&gt;www.example.com&lt;/webhost&gt;
        &lt;database&gt;
            &lt;adapter value=&quot;pdo_mysql&quot;/&gt;
            &lt;params&gt;
                &lt;host value=&quot;db.example.com&quot;/&gt;
                &lt;username value=&quot;dbuser&quot;/&gt;
                &lt;password value=&quot;secret&quot;/&gt;
                &lt;dbname value=&quot;dbname&quot;/&gt;
            &lt;/params&gt;
        &lt;/database&gt;
    &lt;/production&gt;
    &lt;staging extends=&quot;production&quot;&gt;
        &lt;database&gt;
            &lt;params&gt;
                &lt;host value=&quot;dev.example.com&quot;/&gt;
                &lt;username value=&quot;devuser&quot;/&gt;
                &lt;password value=&quot;devsecret&quot;/&gt;
            &lt;/params&gt;
        &lt;/database&gt;
    &lt;/staging&gt;
&lt;/configdata&gt;
</pre>

    </div>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>XML strings</b><br /></span>
        

        <p class="para">
            <span class="classname">Zend_Config_Xml</span> is able to load an <acronym class="acronym">XML</acronym> string
            directly, such as that retrieved from a database. The string is passed
            as the first parameter to the constructor and must start with the
            characters <em class="emphasis">&#039;&lt;?xml&#039;</em>:
        </p>

        <pre class="programlisting brush: xml">
$string = &lt;&lt;&lt;EOT
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;production&gt;
        &lt;db&gt;
            &lt;adapter value=&quot;pdo_mysql&quot;/&gt;
            &lt;params&gt;
                &lt;host value=&quot;db.example.com&quot;/&gt;
            &lt;/params&gt;
        &lt;/db&gt;
    &lt;/production&gt;
    &lt;staging extends=&quot;production&quot;&gt;
        &lt;db&gt;
            &lt;params&gt;
                &lt;host value=&quot;dev.example.com&quot;/&gt;
            &lt;/params&gt;
        &lt;/db&gt;
    &lt;/staging&gt;
&lt;/config&gt;
EOT;

$config = new Zend_Config_Xml($string, &#039;staging&#039;);
</pre>

    </p></blockquote>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Zend_Config XML namespace</b><br /></span>
        

        <p class="para">
            <span class="classname">Zend_Config</span> comes with it&#039;s own <acronym class="acronym">XML</acronym>
            namespace, which adds additional functionality to the parsing process. To take advantage
            of it, you have to define a namespace with the namespace <acronym class="acronym">URI</acronym>
            <var class="filename">http://framework.zend.com/xml/zend-config-xml/1.0/</var> in
            your config root node.
        </p>

        <p class="para">
            With the namespace enabled, you can now use <acronym class="acronym">PHP</acronym> constants within
            your configuration files. Additionally, the <em class="emphasis">extends</em>
            attribute was moved to the new namespace and is deprecated in the
            <b><tt>NULL</tt></b> namespace. It will be completely removed there in
            Zend Framework 2.0.
        </p>

        <pre class="programlisting brush: xml">
$string = &lt;&lt;&lt;EOT
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config xmlns:zf=&quot;http://framework.zend.com/xml/zend-config-xml/1.0/&quot;&gt;
    &lt;production&gt;
        &lt;includePath&gt;
            &lt;zf:const zf:name=&quot;APPLICATION_PATH&quot;/&gt;/library&lt;/includePath&gt;
        &lt;db&gt;
            &lt;adapter value=&quot;pdo_mysql&quot;/&gt;
            &lt;params&gt;
                &lt;host value=&quot;db.example.com&quot;/&gt;
            &lt;/params&gt;
        &lt;/db&gt;
    &lt;/production&gt;
    &lt;staging zf:extends=&quot;production&quot;&gt;
        &lt;db&gt;
            &lt;params&gt;
                &lt;host value=&quot;dev.example.com&quot;/&gt;
            &lt;/params&gt;
        &lt;/db&gt;
    &lt;/staging&gt;
&lt;/config&gt;
EOT;

define(&#039;APPLICATION_PATH&#039;, dirname(__FILE__));
$config = new Zend_Config_Xml($string, &#039;staging&#039;);

echo $config-&gt;includePath; // Prints &quot;/var/www/something/library&quot;
</pre>

    </p></blockquote>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.config.adapters.json.html">Zend_Config_Json</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.config.html">Zend_Config</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.config.adapters.yaml.html">Zend_Config_Yaml</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.config.html">Zend_Config</a></li>
  <li><a href="zend.config.introduction.html">Introduction</a></li>
  <li><a href="zend.config.theory_of_operation.html">Theory of Operation</a></li>
  <li><a href="zend.config.adapters.ini.html">Zend_Config_Ini</a></li>
  <li><a href="zend.config.adapters.json.html">Zend_Config_Json</a></li>
  <li class="active"><a href="zend.config.adapters.xml.html">Zend_Config_Xml</a></li>
  <li><a href="zend.config.adapters.yaml.html">Zend_Config_Yaml</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>